import * as actionTypes from '../constants/index';
import axios from 'axios'

export const saveModules = (data) => {
  return {
    type: actionTypes.GET_MODULES,
    data
  }
};

export const fetchModules = () => {
  return async dispatch => {
    let res = await
      axios.get('/vrv-monitor-platform-web/module/queryByOrgCode')
    if(res.data.statusCode===200){
      dispatch(saveModules(res.data.data))
    }
  }
};
// 模块是否展示在首页
export const showIH=(id,home)=>{
  return {type:actionTypes.SHOW_HOME,id:id,home:home}
};
export const showInHome=(id, home)=>{
  return async dispatch=>{
    let res = await axios.post('vrv-monitor-platform-web/module/changeShow',{
      id,
      home
    });
    if(res.data.success){
      dispatch(showIH(id,home))
    }
  }
};


//初始化数据
export const moduleInforesult = (data,total) => {
  return  {type:actionTypes.MODULE_GET_TAG_LIST,payload:data,total:total}
}
//loading
export const load=(data)=>{
  return {type:actionTypes.MODULE_LOADING,loading:data}
}
// export const changepos=(data)=>{
//   return {type:actionTypes.MODULE_NEGATIVE,positiveOrNegative:data}
// }
export const pageSize=(data)=>{
  return {type:actionTypes.MODULE_PAGESIZE,pagesize:data}
}

//改变筛选条件
export const tagchange=(data)=>{
  return  {type:actionTypes.MODULE_REMOVEACTION,choselist:data}
}
//信息详情跳转
export const detail=(id,key)=>{
  return {type:actionTypes.MODULE_DETAIL,id:id,titleContentPrint:key}
}

//导出所选的id
export const exportId=(deleteIds,)=>{
  return async dispatch=>{
    await window.open('/vrv-monitor-platform-web/data/exportSelects?ids='+deleteIds)
  }
}
//删除所选的id
export const deleteD=(dataid)=>{
  return {type:actionTypes.MODULE_DELETE,id:dataid}
}

export const dataDelete=(
  deleteIds,
)=>{
  return async dispatch=>{
    let res = await axios.post('/vrv-monitor-platform-web/data/delete',{
      deleteIds :deleteIds
    })
    if(res.data.success){
      dispatch(deleteD(deleteIds))
    }
  }
};


export const tabdata = (
  id,
  pageNum,
  pageSize
)=>{
  return async dispatch=>{
    let res = await axios.post('/vrv-monitor-platform-web/module/queryMore',{
      id,
      pageNum,
      pageSize
    })
    dispatch(load(true))
    if(res.data.success){
      localStorage.keywords=res.data.keywords;
      let value=res.data.data
      let data= (value[0] && value[0]['data']) ? value.map(d => ({doc_count: d.doc_count, ...d.data[0]})) : value
      dispatch(moduleInforesult(data,res.data.total))
      dispatch(load(false))
    }
  }
}
